package cn.xspace.wnx.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.wnx.R;
import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.library.view.tree.bean.Node;

public class CommonTreeListAdapber<T> extends TreeListViewAdapter<T> {
    private static final String TAG = "CommonTreeListAdapber";
    public static final int ITEM_PARENT_LAYOUT = 0;
    public static final int ITEM_SUB_LAYOUT = 1;
    private static Context mContext;
    /**
     * 是否展示添加好友
     ***/
    private boolean isShowAdd;
    private boolean isShowCheck;
    private static String user_id;


    public List<Node> getVisibleNodes() {
        return mNodes;
    }

    public List<Node> getAllNodes() {
        return mAllNodes;
    }


    public CommonTreeListAdapber(ListView mTree, Context context, List<T> datas, int defaultExpandLevel, boolean
            isShowAdd, boolean isShowCheck, boolean hasheader) throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
        mContext = context;
        this.hasHeader = hasheader;
        this.isShowCheck = isShowCheck;
        this.isShowAdd = isShowAdd;
        user_id = MyInfoUtil.getMyUserid(mContext);
    }

    public void defaultOpentStatus() {
        for (Node mNode : mAllNodes) {
            if (MyInfoUtil.getUserOrgId().equals(mNode.getId())) {
                //用户本机构默认实际展开的
                mNode.setExpand(true);
                break;
            }
        }
    }

    @Override
    protected void dataControl(ListView mTree) {
        mTree.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (onTreeNodeClickListener != null) {
                    if (iOrgUserCheckListener != null) {
                        View checkbox = view.findViewById(R.id.check_select);
                        if (checkbox != null && checkbox instanceof CheckBox) {
                            boolean state = ((CheckBox) checkbox).isChecked();
                            Node node = mNodes.get(position);
                            OrgDeptUserBean bean = (OrgDeptUserBean) node.getObject();
                            if (user_id.equals(bean.user_id)) {
                                return;
                            }
                            if (!bean.isCheckBoxEnable)
                                return;
                            ((CheckBox) checkbox).setChecked(!state);

                            iOrgUserCheckListener.orgUserCheck(position, bean, !state);
                        } else {
                            LogUtil.d("找不到checkbox");
                        }
                    }
                    onTreeNodeClickListener.onClick(mNodes.get(position), position, false);
                }
            }

        });
    }

    @Override
    public void setData(java.util.List<T> datas, int defaultExpandLevel) {
        super.setData(datas, defaultExpandLevel);
    }

    @Override
    public void setDataSaveState(java.util.List<T> datas, int defaultExpandLevel) {
        super.setDataSaveState(datas, defaultExpandLevel);
    }


    @Override
    public int getItemViewType(int position) {
        OrgDeptUserBean bean = (OrgDeptUserBean) mNodes.get(position).getObject();
//        LogUtil.d(TAG, "bean name >>" + bean.dept_name + "bean postion " + position + " leaf >>" + bean.isLeaf);
        if (!bean.isLeaf) {
            return ITEM_PARENT_LAYOUT;
        } else {
            return ITEM_SUB_LAYOUT;
        }
    }

    @Override
    public List<String> getLastNodeId() {
        return super.getLastNodeId();
    }

    @Override
    public void collapseNodeIdList(List<String> idList) {
        super.collapseNodeIdList(idList);
    }

    @Override
    public View getConvertView(final Node node, final int position, View convertView, ViewGroup parent) {
        LogUtil.d(TAG, "getConvertView");

        ViewHolder holder = null;
        int viewType = getItemViewType(position);
//        LogUtil.d(TAG, "viewType>>" + viewType);
        OrgDeptUserBean bean = (OrgDeptUserBean) node.getObject();
//        LogUtil.d(TAG, "bean == null>>" + (bean == null));
//        LogUtil.d(TAG, "convertView == null>>" + (convertView == null));
//        if (convertView == null) {
        switch (viewType) {
            case ITEM_PARENT_LAYOUT:
                convertView = mInflater.inflate(R.layout.list_item_parent_org, parent, false);
//                    LogUtil.d(TAG, "convertView == null>>" + (convertView == null));
                holder = new ParentViewHolder(convertView);
//                    LogUtil.d(TAG, "holder == null>>" + (holder == null));
                break;
            case ITEM_SUB_LAYOUT:
                convertView = mInflater.inflate(R.layout.list_item_org_user_grey, parent, false);
                holder = new SubViewHolder(convertView);
                break;
        }
//        } else {
//
//            holder = (ViewHolder) convertView.getTag();
//
//        }
//        LogUtil.d(TAG, "holder == null>>" + (holder == null));
        holder.setData(bean, node);
        holder.setListener(bean, node, position);

//        ViewHolder viewHolder = null;
//        UnionFriendsBean ufb = (UnionFriendsBean) node.getObject();
//        if (convertView == null) {
//            convertView = mInflater.inflate(RProxy.list_item_parent_org, parent, false);
//            viewHolder = new ViewHolder();
//            viewHolder.icon = (ImageView) convertView.findViewById(RProxy.id_treenode_icon);
//            viewHolder.label = (TextView) convertView.findViewById(RProxy.id_treenode_label);
//            convertView.setTag(viewHolder);
//            viewHolder.contactInviteTV = (TextView) convertView.findViewById(RProxy.check_org_member_select);
//
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        //
//        viewHolder.icon.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                oit(position, true);
//            }
//        });
//        viewHolder.label.setText(node.getName());
        return convertView;
    }


    private static class ViewHolder {

        public ViewHolder(View view) {
            view.setTag(this);
        }

        protected void setData(OrgDeptUserBean bean, Node node) {

        }

        protected void setListener(OrgDeptUserBean bean, Node node, int position) {

        }
    }


    private class ParentViewHolder extends ViewHolder {
        ImageView iconLeft;
        TextView label;
        ImageView iconRight;
        RelativeLayout rlRight;

        public ParentViewHolder(View view) {
            super(view);
            this.iconLeft = (ImageView) view.findViewById(R.id.img_org_left);
            this.label = (TextView) view.findViewById(R.id.txt_name_org);
            this.iconRight = (ImageView) view.findViewById(R.id.img_org_right);
            this.rlRight = (RelativeLayout) view.findViewById(R.id.rl_org_right);
        }

        @Override
        protected void setData(OrgDeptUserBean bean, Node node) {
            super.setData(bean, node);
            this.label.setText(bean.dept_name);
//            this.iconRight.setImageResource(node.getIcon());
            if (node.isRoot()) { //如果是根节点
                this.iconLeft.setImageResource(R.drawable.icon_top_logo);
            }
            if (!bean.isLookOrg) { //是否是查看机构群组
                this.rlRight.setVisibility(View.GONE);
            }
        }

        @Override
        protected void setListener(final OrgDeptUserBean bean, final Node node, final int position) {
            this.rlRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iParentClickListener != null) {
                        iParentClickListener.onRightClick(bean, node, position, rlRight);
                    }
                }
            });
        }
    }

    private class SubViewHolder extends ViewHolder {
        ImageView headImg;
        TextView nameTxt;
        TextView jobTxt;
        CheckBox checkBox;

        public SubViewHolder(View view) {
            super(view);
            this.headImg = (RoundAngleImageView) view.findViewById(R.id.img_head);
            this.nameTxt = (TextView) view.findViewById(R.id.txt_name);
            this.jobTxt = (TextView) view.findViewById(R.id.txt_job_flag);
            this.checkBox = (CheckBox) view.findViewById(R.id.check_select);
        }

        @Override
        protected void setListener(final OrgDeptUserBean bean, Node node, final int position) {
            super.setListener(bean, node, position);
            this.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if (iOrgUserCheckListener != null) {
//                        iOrgUserCheckListener.orgUserCheck(position, bean, isChecked);
//                    }
                }
            });
            this.checkBox.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOrgUserCheckListener != null) {
                        iOrgUserCheckListener.orgUserCheck(position, bean, ((CheckBox) v).isChecked());
                    }
                }
            });
        }

        @Override
        protected void setData(final OrgDeptUserBean bean, Node node) {
            super.setData(bean, node);
            if (isShowCheck) {
                this.checkBox.setVisibility(View.VISIBLE);
                if (bean.isSelectd) {
                    this.checkBox.setChecked(true);
                } else {
                    this.checkBox.setChecked(false);
                }
                this.checkBox.setEnabled(bean.isCheckBoxEnable);
                if (bean.isDeptAllUser) {
                    this.headImg.setVisibility(View.GONE);
                }

//                if (user_id.equals(bean.user_id)) {
//                    this.checkBox.setVisibility(View.INVISIBLE);
//                }
            }
            this.nameTxt.setText(bean.dept_name);
            if (bean.isGroupChat) { //是否群组,目前已无用
                this.headImg.setImageResource(R.drawable.icon_default_org);
            } else {
                HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, this.headImg, HeadShowUtil.HeadType
                        .UserLogo);
            }

//
//            if (!TextUtils.isEmpty(bean.user_logo)) {
//                Picasso.with(mContext)
//                        .load(bean.user_logo)
//                        .placeholder(R.drawable.icon_header_default)
//                        .error(R.drawable.icon_header_default)
//                        .into(this.headImg);
//            } else {
////                this.headImg.setImageResource(R.drawable.icon_header_default);
//            }

            if ("0".equals(bean.isMainDept)) { //是否兼职
                this.jobTxt.setVisibility(View.VISIBLE);
            } else {
                this.jobTxt.setVisibility(View.GONE);
            }

        }

    }

    private void viewOnclick(View v, final Node node) {
        v.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, node.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private IOrgUserCheckListener iOrgUserCheckListener;

    public void setiOrgUserCheckListener(IOrgUserCheckListener iOrgUserCheckListener) {
        this.iOrgUserCheckListener = iOrgUserCheckListener;
    }

    private IParentClickListener iParentClickListener;

    public void setiParentClickListener(IParentClickListener iParentClickListener) {
        this.iParentClickListener = iParentClickListener;
    }


    public interface IOrgUserCheckListener {
        void orgUserCheck(int postion, OrgDeptUserBean bean, boolean isChecked);
    }

    public interface IParentClickListener {
        void onRightClick(OrgDeptUserBean bean, Node node, int position, RelativeLayout iconRight);
    }


}
